unit uStockMasuk;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, BaseGrid, AdvGrid, ExtCtrls, AdvEdit, AdvCombo,
  RzLabel, RzButton, uDM, DB, MemDS, DBAccess, Uni, RzBckgnd, ComCtrls,
  AdvDateTimePicker, AdvEdBtn, AdvObj;

type
  TFrmStockMasuk = class(TForm)
    RzSeparator1: TRzSeparator;
    RzSeparator2: TRzSeparator;
    Label1: TLabel;
    Panel1: TPanel;
    RzLabel2: TRzLabel;
    RzLabel1: TRzLabel;
    NoReff: TAdvEdit;
    QtyStock: TAdvEdit;
    grid: TAdvStringGrid;
    BtnSave: TRzBitBtn;
    BtnCancel: TRzBitBtn;
    BtnEdit: TRzBitBtn;
    BtnDelete: TRzBitBtn;
    RzBitBtn5: TRzBitBtn;
    Tgl: TAdvDateTimePicker;
    KodeStock: TAdvEditBtn;
    NamaStock: TAdvEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure RoomTypeKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure RzButton5Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure RzBitBtn5Click(Sender: TObject);
    procedure BtnEditClick(Sender: TObject);
    procedure BtnSaveClick(Sender: TObject);
    procedure BtnNewClick(Sender: TObject);
    procedure RoomTypeClick(Sender: TObject);
    procedure KodeStockClickBtn(Sender: TObject);
    procedure KodeStockKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure TglKeyPress(Sender: TObject; var Key: Char);
    procedure BtnDeleteClick(Sender: TObject);
  private
    { Private declarations }
    procedure initForm;
    Procedure initGrid;
    Procedure LoadData(ID: String);
    Procedure LoadFromData(ID: String);
    procedure setEnableDisable;
    procedure lookupRoomType;

    function isValid: boolean;
    function isSaved: boolean;
    function getMaxRoom: integer;
  public
    { Public declarations }
  end;

var
  FrmStockMasuk: TFrmStockMasuk;
CONST
  colNO     = 0;
  colTgl    = 1;
  colRef    = 2;
  colStock  = 3;
  colQty    = 4;
  colSatuan = 5;

implementation

{$R *.dfm}

procedure TFrmStockMasuk.Button1Click(Sender: TObject);
begin
  Close;
end;

procedure TFrmStockMasuk.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:= caFree;
end;

procedure TFrmStockMasuk.FormShow(Sender: TObject);
begin
  initForm;
  LoadData('');
//  lookupRoomType;
end;

function TFrmStockMasuk.getMaxRoom: integer;
begin
  OpenSQL(DM.Buffer, 'select count(id_room) from room');
  if DM.Buffer.RecordCount > 0 then
    Result := DM.Buffer.Fields[0].AsInteger
  else
    Result := 0;
end;

procedure TFrmStockMasuk.initForm;
begin
  NoReff.Tag := 0;
  NoReff.Clear;
  KodeStock.Clear;
  QtyStock.Clear;
  Tgl.Date := serverNow;
  setEnableDisable;
  NoReff.SetFocus;
end;

procedure TFrmStockMasuk.initGrid;
begin
  grid.AutoSize := True;
//  grid.Alignments[colNama, 0] := taCenter;
//  grid.Alignments[colTipe, 0] := taCenter;
//  grid.Alignments[colIP, 0] := taCenter;
end;

function TFrmStockMasuk.isSaved: boolean;
begin
  Result := True;
  try
    if NoReff.Tag = 0 then
      ExecSQL(DM.Buffer, 'insert into stock_masuk (Noref, Tanggal, IdStock, Qty) '+
      'values ('+QuotedStr(NoReff.Text)+','+QuotedStr(FormatDateTime('yyyy-mm-dd',Tgl.Date))+','+
      IntToStr(KodeStock.Tag)+','+QtyStock.Text+')')
    else
      ExecSQL(DM.Buffer, 'update stock_masuk set '+
      'Tanggal='+QuotedStr(FormatDateTime('yyyy-mm-dd',Tgl.Date))+','+
      'IdStock='+IntToStr(KodeStock.Tag)+','+
      'Qty='+QtyStock.Text+' where Noref='+QuotedStr(NoReff.Text));
    LoadData('');
    if not Confirm('Data Sukses Disimpan.'+#13+'Input Lagi?') then
      initForm
    else
      KodeStock.SetFocus;
  except
    Result := false;
  end;
end;

function TFrmStockMasuk.isValid: boolean;
begin
  Result:= False;
  if CekInput(Result, NoReff.Text <> '', 'Kode Room belum diisi.', NoReff) then
  if CekInput(Result, KodeStock.Text <> '', 'Nama Room belum diisi.', KodeStock) then
//  if CekInput(Result, RoomType.ItemIndex <> -1, 'Tipe Room belum sesuai.', RoomType) then
      CekInput(Result, QtyStock.Text <> '', 'IP Address belum sesuai.', QtyStock);
end;

procedure TFrmStockMasuk.KodeStockClickBtn(Sender: TObject);
begin
  OpenSQL(DM.Lookup, 'select IdStock as ID, KodeStock as Kode, NamaStock as Deskripsi from stock');
  DM.Lookup.ReadOnly := True;
  LoadFormLookup;
  KodeStock.Tag := DM.Lookup.Fields[0].AsInteger;
  KodeStock.Text := DM.Lookup.Fields[1].AsString;
  NamaStock.Text := DM.Lookup.Fields[2].AsString;
end;

procedure TFrmStockMasuk.KodeStockKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key=vk_f2 then
    KodeStockClickBtn(Sender);
end;

procedure TFrmStockMasuk.LoadData(ID: String);
var i : integer;
begin
//  initForm;
  OpenSQL(DM.Buffer , 'select m.tanggal,m.noref,s.NamaStock,m.qty,s.SatuanStock '+
  'from stock_masuk m join stock s on s.IDStock=m.IDStock');
  if DM.Buffer.RecordCount > 0 then
  begin
    with grid do
    begin
       for i := 1 to DM.Buffer.RecordCount do
       begin
          RowCount := i + 1;
          Ints[colNO, i] := i;
          Cells[colTgl, i] := DM.Buffer.Fields[0].AsString;
          Cells[colRef, i] := DM.Buffer.Fields[1].AsString;
          Cells[colStock, i] := DM.Buffer.Fields[2].AsString;
          Cells[colQty, i] := FloatToStrFmt(DM.Buffer.Fields[3].Value, true, 0);
          Cells[colSatuan, i] := DM.Buffer.Fields[4].AsString;
            if i mod 2 = 1 then
              RowColor[i] := clMenu;
          DM.Buffer.Next;
       end;
    end;
//    LbInfo.Caption := 'JUMLAH ROOM = '+IntToStr(DM.Buffer.RecordCount);
  end;
  initGrid;
end;

procedure TFrmStockMasuk.LoadFromData(ID: String);
begin
//  OpenSQL(DM.Buffer , 'select m.tanggal,m.noref,s.KodeStock,s.NamaStock,m.qty,s.SatuanStock '+
//  'from stock_masuk m join stock s on s.KodeStock=m.KodeStock where m.noref='''+ID+'''');
//  if DM.Buffer.RecordCount > 0 then
//  begin
//    Tgl.Date := DM.Buffer.Fields[0].AsDateTime;
//    NoReff.Text := DM.Buffer.Fields[1].AsString;
//    KodeStock.Text := DM.Buffer.Fields[2].AsString;
//    NamaStock.Text := DM.Buffer.Fields[3].AsString;
//    QtyStock.Text := DM.Buffer.Fields[4].AsString;
//    NoReff.Tag := 1;
//  end else
//    initForm;
//  setEnableDisable;
end;

procedure TFrmStockMasuk.lookupRoomType;
var i : integer;
begin
//  RoomType.Clear;
//  RoomType.Items.Add('Add New Item ...');
  OpenSQL(DM.Buffer,'select nama from room_kategory order by id_kategory');
  if DM.Buffer.RecordCount > 0 then
    while not DM.Buffer.Eof do
    begin
//      RoomType.Items.Add(DM.Buffer.Fields[0].AsString);
      DM.Buffer.Next;
    end;
end;

procedure TFrmStockMasuk.RoomTypeClick(Sender: TObject);
begin
//  if RoomType.ItemIndex = 0 then
  begin
    OpenSQL(DM.Lookup, 'select nama as Kategori,rate as Harga, Discount  from room_kategory');
    LoadFormLookup;
    lookupRoomType;
  end;  
end;

procedure TFrmStockMasuk.RoomTypeKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key=vk_return then
     SelectNext(ActiveControl, true, true);
end;

procedure TFrmStockMasuk.BtnSaveClick(Sender: TObject);
begin
  if (isValid) //and (getMaxRoom < 181)
   then isSaved;
end;

procedure TFrmStockMasuk.BtnDeleteClick(Sender: TObject);
begin
  if Confirm(MSG_CONFIRM_DELETE) then
  begin
    ExecSQL(DM.Buffer, 'delete from stock_masuk where noref='+QuotedStr(NoReff.Text));
    LoadData('');
  end;
end;

procedure TFrmStockMasuk.BtnEditClick(Sender: TObject);
begin
  LoadFromData(grid.Cells[colRef, grid.Row]);
end;

procedure TFrmStockMasuk.RzBitBtn5Click(Sender: TObject);
begin
  close;
end;

procedure TFrmStockMasuk.BtnNewClick(Sender: TObject);
begin
  initForm;
end;

procedure TFrmStockMasuk.RzButton5Click(Sender: TObject);
begin
  close;
end;

procedure TFrmStockMasuk.setEnableDisable;
begin
  NoReff.ReadOnly := (NoReff.Tag = 1);
  BtnCancel.Enabled := (NoReff.Tag > 0);
  BtnEdit.Enabled := (NoReff.Tag = 0);
  BtnDelete.Enabled := (NoReff.Tag > 0);
  NamaStock.ReadOnly:=True;
end;

procedure TFrmStockMasuk.TglKeyPress(Sender: TObject; var Key: Char);
begin
  if key=#13 then
    SelectNext(ActiveControl, true, true);
end;

end.
